//
//  ViewController.m
//  UITableView-多组数据显示
//
//  Created by hyl on 16/5/21.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"
#import "HYLCar.h"
#import "HYLGroup.h"

@interface ViewController ()<UITableViewDataSource>
@property (nonatomic,strong) NSArray *groups;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
#pragma mark - setter

-(NSArray *)groups{
    if (_groups==nil) {
        NSMutableArray *mGroups=[NSMutableArray array];
        
        HYLGroup *group1=[[HYLGroup alloc]initWithHeadName:@"德系" footName:@"德系三强BBA还是ABB?"];
        group1.cars=@[
                     [[HYLCar alloc]initWithName:@"宝马" icon:@"m_3_100"],
                     [[HYLCar alloc]initWithName:@"奥迪" icon:@"m_2_100"],
                     [[HYLCar alloc]initWithName:@"奔驰" icon:@"m_9_100"]
                     ];
        [mGroups addObject:group1];
        
        HYLGroup *group2=[[HYLGroup alloc]initWithHeadName:@"天朝" footName:@"比亚迪还有哪些?"];
        group2.cars=@[
                      [[HYLCar alloc]initWithName:@"比亚迪" icon:@"m_15_100"],
                      [[HYLCar alloc]initWithName:@"什么车" icon:@"m_16_100"]
                      ];
        [mGroups addObject:group2];
        _groups=mGroups;
    }
    return _groups;
}

#pragma mark - <UITableViewDataSource>

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return self.groups.count;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.groups[section] cars].count;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc]init];
    HYLGroup *group=self.groups[indexPath.section];
    HYLCar *car=group.cars[indexPath.row];
    cell.textLabel.text=car.name;
    cell.imageView.image=[UIImage imageNamed:car.icon];
    
    return cell;
}


-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    HYLGroup *group=self.groups[section];
    return group.footName;
}
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    HYLGroup *group=self.groups[section];
    return group.headName;
}

@end
